package com.sgb.kjwl.view.ui.adapter.home;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.GoodsInfoEntity;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.utils.VehicleConstant;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.StringUtil;

import java.math.BigDecimal;
import java.util.List;

public class SourceOfGoodsAdapter extends BaseRecycleAdapter<GoodsInfoEntity> {

    public SourceOfGoodsAdapter(Context context, List<GoodsInfoEntity> data) {
        super(context, R.layout.item_source_of_goods, data);
    }

    @Override
    public void convert(ItemViewHolder holder, GoodsInfoEntity entity) {
        holder.setBinding(BR.viewModel, entity);

        TextView tvDistance = holder.getItem().findViewById(R.id.tv_distance);
        if (entity != null) {
            if (TextUtils.isEmpty(entity.getDistance())) {
                tvDistance.setText("");
            } else {
                tvDistance.setText("据您  " + (Double.valueOf(entity.getDistance()) < 1000 ? "1km以内" : StringUtil.fromatUpDistance(entity.getDistance()) + "KM"));
            }
        }
        TextView type_name = holder.getView(R.id.type_name);
        if (TextUtils.isEmpty(entity.getWeight())){
            type_name.setText(entity.getGoodsModeName());
        }else {
            BigDecimal weight = new BigDecimal(entity.getWeight()).setScale(2);
            type_name.setText(entity.getGoodsModeName() + "/" + weight);
        }
        TextView content_view = holder.getView(R.id.content_view);
        String carModelNameStr = TextUtils.isEmpty(entity.getCarModelName()) ? "" : entity.getCarModelName();
        String carLenStr = TextUtils.isEmpty(entity.getCarLenName()) ? "" : " " + entity.getCarLenName();
        String heightHurdleStr = getValidHeightHurdle(entity.getHeightHurdle());
        String dumpStr = TextUtils.isEmpty(VehicleConstant.getIsDumpStr(entity.getIsDump())) ? "" : " " + VehicleConstant.getIsDumpStr(entity.getIsDump());
        content_view.setText(carModelNameStr + carLenStr + heightHurdleStr + dumpStr);
        TextView tvJourney = holder.getView(R.id.transport_distance);
        if (TextUtils.isEmpty(entity.getJourney())) {
            tvJourney.setText("");
            tvJourney.setVisibility(View.GONE);
        } else {
            String journey = Double.valueOf(entity.getJourney()) < 1000 ? "1km以内" : StringUtil.fromatUpDistance(entity.getJourney()) + "km";
            tvJourney.setText("运程 " + journey);
        }
        TextView price_view = holder.getView(R.id.price_view);
        price_view.setText(Html.fromHtml("运费 " + NumberUtil.trim0(entity.getAllFreight()) + "元"));
        TextView shipmentTime = holder.getItem().findViewById(R.id.shipment_time);
        if (entity.getShipmentEndTime() == null){
            shipmentTime.setText("装货时间  " + DateUtils.stampToDay4(String.valueOf(entity.getShipmentTime())));
        }else {
            shipmentTime.setText("装货时间  " + DateUtils.stampToDay4(String.valueOf(entity.getShipmentTime())) + " - " + DateUtils.stampToDay4(entity.getShipmentEndTime()));
        }
        TextView tv_company = holder.getView(R.id.tv_company);
        tv_company.setText(entity.getCompanyName());
        RoundedImageView iv_logo = holder.getView(R.id.iv_logo);
        TextView role = holder.getView(R.id.role);
        if (!TextUtils.isEmpty(entity.getTerminal())) {
            role.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(entity.getCompanyName()))
                tv_company.setText(entity.getCompanyName());
            switch (entity.getTerminal()) {
                case "1":
                    role.setVisibility(View.VISIBLE);
                    role.setBackgroundResource(R.drawable.bg_blue_solid_ring);
                    role.setTextColor(Color.parseColor("#5792FD"));
                    role.setText("企业");
                    iv_logo.setOval(false);
                    iv_logo.setCornerRadius(4);
                    break;
                case "2":
                    role.setVisibility(View.VISIBLE);
                    role.setBackgroundResource(R.drawable.bg_blue_solid_ring);
                    role.setTextColor(Color.parseColor("#5792FD"));
                    role.setText("团队");
                    iv_logo.setOval(false);
                    iv_logo.setCornerRadius(4);
                    break;
                case "3":
                    role.setVisibility(View.VISIBLE);
                    role.setBackgroundResource(R.drawable.bg_blue_solid_ring);
                    role.setTextColor(Color.parseColor("#5792FD"));
                    role.setText("货主");
                    iv_logo.setOval(false);
                    iv_logo.setCornerRadius(4);
                    break;
            }
        }
    }

    private String getValidHeightHurdle(String heightHurdle) {
        if (TextUtils.isEmpty(heightHurdle) || heightHurdle.equals("0.0")) {
            return "";
        }
        return " 栏高 " + NumberUtil.trim0(heightHurdle) + "米";
    }
}
